﻿using System.Web.Services;
using System.Web.Script.Services;
using eShop.Utility;
using System;
using eShop.Model;
using eShop.Business;
using eShop.Business;
using Entity;

/// <summary>
/// Summary description for Utilities
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Utilities : System.Web.Services.WebService
{

    public Utilities()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
    public string ResolveUrl(string url)
    {
        string resolvedUrl = ImageResizer.CheckImageMissing(url, eShop.Business.SiteSettings.NoImageUrl);
        if (!resolvedUrl.Contains("http://"))
            resolvedUrl = SiteGlobals.BaseUrl + resolvedUrl.Replace("~/", "");
        return resolvedUrl;
    }

    [WebMethod(EnableSession = true)]
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
    public void AddToCart(Int32 qty, Int32 _productId, int stockId)
    {
        ShoppingCartManager.AddProductToCart(_productId, stockId, qty);
    }

    [WebMethod(EnableSession = true)]
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
    public bool AddToWishList(Int32 _productId)
    {
        if (SessionManager.IsUserLoggedIn)
        {
            TblFavorites favorite = new TblFavorites();
            favorite.ProductId = _productId;
            favorite.UserId = SessionManager.LoggedUser.Id;
            favorite.IsActive = true;
            favorite.CreateDate = DateTime.Now;
            favorite.Save();
            return true;
        }
        else
        {
            return false;
        }
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
    public static bool SignUpSubscriber(string email)
    {
        if (RegExUtil.IsEmail(email))
        {
            NewsletterSubscriber newSubscriber = new NewsletterSubscriber(0, email.Substring(0, email.IndexOf("@")), email, 1);
            newSubscriber.Update();
            return true;
        }
        else
        {
            return false;
        }
    }
}
